<link rel="stylesheet" type="text/css" href="<?=base_url();?>templates/css/ui/colorbox.css" />
<script type="text/javascript">
    var arr_obj_img = new Array();
    
    function auto_search(url_search)
    {
        var key_search = $("#key_search").val();
        $.ajax({
            type: "POST",
            url: "<?=base_url()?>"+url_search,
            data: {key_search:key_search,field_search:$("#field_search").val(),type:"ajax",image_category_id:$("#image_category_id").val()},
            cache: false,
            dataType: "html",
            success: function(data){
                $("#tbl_file_search").html(data);
            }
        });
    }
    function insert(data)
	{
        parent.imgLib.insert(data);
        parent.$.fn.colorbox.close();
        /*var rt_html = "";
        rt_html += '<input type="hidden" name="<?=$return_tag?>" value="images/'+file+'" id="<?=$return_tag?>"/>';
        rt_html += '<a href="<?=base_url()?>mod_file_manager/image_manager/cropimage/<?=$return_tag?>/<?=$width?>/<?=$height?>" class="cboxElement" title="<?=lang('file.image_crop');?>"><img src="<?=base_url_site().'uploads/images/'?>'+file+'" width="100px"/></a>';
        $(".<?=$return_tag?>", top.document).html(rt_html);
        parent.$(".cboxElement").colorbox({width:"950", height:"650",iframe:true});
        parent.$.fn.colorbox.close();*/
    }
    function edit_image(img_id){
        $.colorbox({
            title:"Edit image",
            href:"<?=base_url().$this->_module_name.'/image_manager/edit_image/';?>"+img_id,
            iframe:true, 
            width:"550px", 
            height:"200px"
            ,onClosed:function(){
                 document.location.reload();
            }
        });
    }
    $(document).ready(function(){
        $("#insert").click(
            function(){
                var arr_chose = get_check_chose('ar_id[]','checknew');
                var arr_rt = new Array();
                if(arr_chose.length>0){
                    for(var i=0;i<arr_chose.length;i++){
                        arr_rt.push(arr_obj_img[arr_chose[i]]);
                    }
                    insert(arr_rt);
                }else{
                    alert('No image is chose!');
                }
            }
        );
        
    });
</script>
<? 
    $seg2 = $this->router->fetch_method();
?>

<div class="media-upload-header">
    <ul id="menu">
        <li><a href="<?=base_url()?>mod_file_manager/image_manager/index/<?=$return_tag.'/'.$width.'/'.$height?>" <?if($seg2=='index'){echo ' class="current"';}?>><?=lang('library');?></a></li>
        <li><a href="<?=base_url()?>mod_file_manager/image_manager/upload/<?=$return_tag.'/'.$width.'/'.$height?>" <?if($seg2=='upload'){echo ' class="current"';}?>><?=lang('upload_file');?></a></li>
    </ul>
</div>
<div style="padding: 20px;height: 500px; overflow: auto;">
    <div style="text-align: center;width: 100%;" class="image_search_bar">
        <input type="button" id="insert" name="insert" value="Insert"/>
        <select style="float:left;" name="image_category_id" id="image_category_id" onChange="auto_search('mod_file_manager/image_manager/index/<?=$return_tag?>');">
            <option value="">All</option>
            <?php foreach($list_category as $category){
                echo '<option value="'.$category->image_category_id.'">'.$category->image_category_name.'</option>';
            }?>
        </select>
        <?php echo $search_bar;?>
    </div>
    <?
        $attributes = array('class' => 'email', 'id' => 'checknew');
        echo form_open($this->_module_name.'/image_manager/dels/'.$return_tag.'/'.$width.'/'.$height.'/'.(int)$current_page, $attributes);
    ?>
    <div id="tbl_file_search">
        <div class="clear"></div>
        <table  class="site">
            <tr>
                <td colspan="5"> 
                    <? //if($this->acl->check('dels')){ ?>
                        <input type="submit" onclick="return verify_del();" name="btn_submit" class="submit" value="<?=lang('admin.deletes');?>">
                    <? //} ?>
                    <?=lang('admin.total');?>
                    <?=$total?>
                    <?=lang('admin.records');?>
                    <span class="pages">
                    <?=$pagination?>
                    </span> 
                </td>
            </tr>
            <tr>
                <td width="20px"><input type="checkbox" name="sa" id="sa" onClick="check_chose('sa', 'ar_id[]', 'checknew')" /></td>
                <td>Image</td>
                <td>Title</td>
                <td>Category</td>
                <td>Delete</td>
            </tr>
   
                <? foreach($list as $rs): ?>
            <script>
                var img = new Object();
                    img.image_data_id="<?=$rs->image_data_id?>";
                    img.image_category_id="<?=$rs->image_category_id?>";
                    img.lb_title="<?=$rs->lb_title?>";
                    img.description="<?=$rs->description?>"; 
                    img.lb_name="<?=$rs->lb_name?>"; 
                    img.lb_dir="<?=$rs->lb_dir?>"; 
                    img.lb_size="<?=$rs->lb_size?>"; 
                    img.lb_ext="<?=$rs->lb_ext?>";
                    img.dt_create="<?=$rs->dt_create?>";
                arr_obj_img['<?=$rs->image_data_id?>']=img;
            </script>
            <tr>
                <td><input type="checkbox" name="ar_id[]" value="<?=$rs->image_data_id?>"></td>
                <td><a href="javascript:;" onClick="insert(Array(arr_obj_img['<?=$rs->image_data_id?>']));"><?=$this->icon_library->loadtypefile('uploads/images/'.$rs->lb_dir.'/'.$rs->lb_name,$rs->lb_ext)?></a></td>
                <td><a href="javascript:;" onClick="insert(Array(arr_obj_img['<?=$rs->image_data_id?>']));"><?=$rs->lb_title?></a></td>
                <td><a href="javascript:;" onClick="insert(Array(arr_obj_img['<?=$rs->image_data_id?>']));"><?=$rs->image_category_name?></a></td>
                <td>
                    <?=icon_del($this->_module_name.'/image_manager/del/'.$rs->image_data_id.'/'.$return_tag.'/'.$width.'/'.$height.'/'.(int)$current_page);?>
                    <a onclick="edit_image('<?=$rs->image_data_id?>');" href="javascript:void(0);" title="Edit"><img src="<?=base_url().'templates/icon/edit.gif';?>"></a>
                </td>
            </tr>
        <? endforeach;?>
            <tr>
                <td colspan="5"> 
                    <? //if($this->acl->check('dels')){ ?>
                        <input type="submit" onclick="return verify_del();" name="btn_submit" class="submit" value="<?=lang('admin.deletes');?>">
                    <? //} ?>
                    <?=lang('admin.total');?>
                    <?=$total?>
                    <?=lang('admin.records');?>
                    <span class="pages">
                    <?=$pagination?>
                    </span> 
                </td>
            </tr>
        </table>
    </div>
    <?=form_close()?>
</div>